package org.stream;

import org.entity.Employee;
import org.junit.jupiter.api.Test;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;

/**
 * @Description
 * @Author wjj
 * @Date 2023/4/17 22:23
 */
public class Reduce {

//    T  reduce(T identity, BinaryOperator<T> accumulator)
//    <U> U reduce(U identity, BiFunction<U,? super  T,U> accumulator, BinaryOperator<U> combiner)
//    Optional<T> reduce(BinaryOperator<T> accumulator)

    @Test
    public void reduceTest(){
        Integer sum = Arrays.asList(1, 2, 3, 4, 5).stream()
                .reduce(0, Integer::sum);
        System.out.println(sum);
    }

    @Test
    public void reduceTest2(){
        Double doubleSum = Employee.persons().stream()
                .map(Employee::getIncome)
                .reduce(0.0, Double::sum);
        System.out.println(doubleSum);
    }

//    <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
    @Test
    public void reduceTest3(){
        Double doubleSum = Employee.persons().stream()
                .reduce(0.0, (pSum,person)->pSum+person.getIncome(),Double::sum);
        System.out.println(doubleSum);
    }

    @Test
    public void reduceTest4(){
        double sum = Employee
                .persons()
                .stream()
                .reduce(
                        0.0,
                        (Double partialSum, Employee p) -> {
                            double accumulated = partialSum + p.getIncome();
                            System.out.println(Thread.currentThread().getName()
                                    + "  - Accumulator: partialSum  = " + partialSum
                                    + ",  person = " + p + ", accumulated = " + accumulated);
                            return accumulated;
                        },
                        (a, b) -> {
                            double combined = a + b;
                            System.out.println(Thread.currentThread().getName()
                                    + "  - Combiner:  a  = " + a + ", b  = " + b
                                    + ", combined  = " + combined);
                            return combined;
                        });
        System.out.println("--------------------------------------");
        System.out.println(sum);

        sum = Employee
                .persons()
                .parallelStream()
                .reduce(
                        0.0,
                        (Double partialSum, Employee p) -> {
                            double accumulated = partialSum + p.getIncome();
                            System.out.println(Thread.currentThread().getName()
                                    + "  - Accumulator: partialSum  = " + partialSum
                                    + ",  person = " + p + ", accumulated = " + accumulated);
                            return accumulated;
                        },
                        (a, b) -> {
                            double combined = a + b;
                            System.out.println(Thread.currentThread().getName()
                                    + "  - Combiner:  a  = " + a + ", b  = " + b
                                    + ", combined  = " + combined);
                            return combined;
                        });
        System.out.println(sum);
    }

    @Test
    public void reduceTest5(){
        Optional<Integer> max = Stream.of(1, 2, 3, 4).reduce(Integer::max);
        String maxNum = max.isPresent() ? "max=" + max.get() : "max is not defined";
        System.out.println(maxNum);

        //如果流为空，则不能使用默认值0。
        max = Stream.<Integer> empty().reduce(Integer::max);
        if (max.isPresent()) {
            System.out.println("max = " + max.get());
        } else {
            System.out.println("max is not  defined.");
        }
    }

    @Test
    public void reduceTest6(){
        Optional<Employee> person = Employee.persons()
                .stream()
                .reduce((p1, p2) -> p1.getIncome() > p2.getIncome() ? p1 : p2);
        if (person.isPresent()) {
            System.out.println("Highest earner: " + person.get());
        } else {
            System.out.println("Could not  get   the   highest earner.");
        }
    }
}
